<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\core\Controller;
use herosphp\http\HttpRequest;
use herosphp\session\Session;
use herosphp\utils\AjaxResult;

/**
 * login action
 * @package admin\action
 * @author yangjian<yangjian102621@gmail.com>
 */
class LoginAction extends Controller {

    protected $serviceBean = 'admin.manager.service';

    //登陆页面
    public function index(HttpRequest $request) {
        $service = Beans::get($this->serviceBean);
        $new = $request->getParameter("new");
        if ( $service->isLogin() ) {
            location('/admin/offer/index');
        }
        //var_dump($new);die;
        if(!$new) {
            $this->setView("system/login");
        }else {
            $this->setView("system/login_new");
        }
        
    }

    //登陆验证
    public function signin(HttpRequest $request) {
        $username = $request->getParameter('uname');
        $password = $request->getParameter('pwd');
        $scode = $request->getParameter('scode');

        Session::start();
        if ( strtoupper($scode) != $_SESSION['scode'] ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '验证码错误');
        }

        $service = Beans::get($this->serviceBean);
        $user = $service->login($username, $password);
        if ( $user == false ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '登陆失败，用户名或者密码错误');
        }

        if ( $user['status'] == 0 ) {
            $service = Beans::get($this->serviceBean);
            $service->logout();
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '你的账户已被禁用，请联系管理员');
        }

        $request->addParameter('logmessage', "{$username} 登陆了系统");
        AjaxResult::ajaxSuccessResult();

    }

    //退出登陆
    public function logout(HttpRequest $request) {
        $service = Beans::get($this->serviceBean);
        $service->logout();
        location("/admin/login/index");
    }
}
